package com.ifp.opengate.boot.validator.service;

import java.math.BigDecimal;

import org.springframework.stereotype.Component;

import com.ifp.opengate.boot.bean.ValidateEntity;
import com.ifp.opengate.boot.bean.ValidatorForm;
import com.ifp.opengate.boot.validator.constants.ValidatorConstants;

/**
 * @description: 最小值校验 支持 string,int,long,short,float,double,BigDecimal类型的数字对比
 * @author: yangquan
 * @create: 2020/3/14
 */
@Component
public class MinValidateServiceImpl extends ValidateServiceImpl {

    @Override
    public ValidateEntity validateValue(ValidatorForm validatorForm, Object data) {
        if (data == null) {
            return null;
        }
        String message = validatorForm.getErrorMsg();
        long value = Long.parseLong(validatorForm.getCheckPattern());
        if (data instanceof Integer && value < (Integer)data) {
            return null;
        } else if (data instanceof Double && value < (Double)data) {
            return null;
        } else if (data instanceof Float && value < (Float)data) {
            return null;
        } else if (data instanceof Long && value < (Long)data) {
            return null;
        } else if (data instanceof String && value < Long.parseLong((String)data)) {
            return null;
        } else if (data instanceof BigDecimal && (new BigDecimal(value + "").compareTo((BigDecimal)data) < -1)) {
            return null;
        } else if (data instanceof Short && value < (Short)data) {
            return null;
        }
        return new ValidateEntity(ValidatorConstants.DEFAULT_ERROR_CODE, message, data, validatorForm.getParamName());
    }
}